﻿using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Admin.Web.Controllers;
using YiSha.Basics;
using YiSha.Entity.OrganizationManage;
using YiSha.Framework;
using YiSha.Model.Param.OrganizationManage;
using YiSha.Service.OrganizationManage;
using YiSha.Util;
using YiSha.Util.Model;

namespace YiSha.Admin.Web.Areas.OrganizationManage.Controllers
{
    [Area("OrganizationManage")]
    public class NewsController : BaseController
    {
        public NewsService NewsService { get; set; }
        #region 视图功能
        [AuthorizeFilter("organization:news:view")]
        public IActionResult NewsIndex()
        {
            return View();
        }

        public async Task<IActionResult> NewsForm()
        {
            ViewBag.OperatorInfo = await OperatorService.Instance.Current();
            return View();
        }
        #endregion

        #region 获取数据
        [HttpGet]
        [AuthorizeFilter("organization:news:search")]
        public async Task<IActionResult> GetListJson(NewsListParam param)
        {
            var obj = await NewsService.GetList(param);
            return Json(obj);
        }

        [HttpGet]
        [AuthorizeFilter("organization:news:search")]
        public async Task<IActionResult> GetPageListJson(NewsListParam param, Pagination pagination)
        {
            var obj = await NewsService.GetPageList(param, pagination);
            return Json(obj);
        }

        [HttpGet]
        public async Task<IActionResult> GetFormJson(string id)
        {
            TData<NewsEntity> obj = await NewsService.GetEntity(id);
            return Json(obj);
        }

        #endregion

        #region 提交数据
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="areaId">区域编号以逗号分割</param>
        /// <returns></returns>
        [HttpPost]
        [AuthorizeFilter("organization:news:add,organization:news:edit")]
        public async Task<IActionResult> SaveFormJson(NewsEntity entity)
        {
            if (!entity.AreaId.IsEmpty())
            {
                var arr = entity.AreaId.Split(",");
                entity.ProvinceId = arr[0];
                if (arr.Length>1)
                {
                    entity.CityId = arr[1];
                }
                if (arr.Length > 2)
                {
                    entity.CountyId = arr[2];
                }
            }
            TData<string> obj = await NewsService.SaveForm(entity);
            return Json(obj);
        }

        [HttpPost]
        [AuthorizeFilter("organization:news:delete")]
        public async Task<IActionResult> DeleteFormJson(List<string> ids)
        {
            TData obj = await NewsService.DeleteForm(ids);
            return Json(obj);
        }
        #endregion
    }
}
